﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web;

/// <summary>
///MailClass 的摘要说明
/// </summary>
public class Mail
{
    private static readonly string _connectionString;

    private int m_iID;
    public int ID 
    {
        get { return m_iID;}
    }

    private int m_iSenderID;
    public int SenderID
    {
        get { return m_iSenderID; }
    }  

    private int m_iRecieverID;
    public int RecieverID
    {
        get { return m_iRecieverID; }
    }     

    private DateTime m_dtDate;
    public DateTime Date
    {
        get { return m_dtDate; }
    }

    private string  m_sTitle;
    public string Title
    {
        get { return m_sTitle; }
    }

    private string m_sDetail;
    public string Detail
    {
        get { return m_sDetail; }
    }

	public Mail(int id, int senderid, int receiveid
        , DateTime date, string title, string detail)
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
        m_iID = id;
        m_iRecieverID = receiveid;
        m_iSenderID = senderid;
        m_dtDate = date;
        m_sTitle = title;
        m_sDetail = detail;
	}

    //1)添加一条站内信到数据库		静态
    //static void AddMail(id , SenderID , RecieverID , date , title , detail) 
    /// <summary>
    /// 增加一条站内信
    /// </summary>
    /// <param name="userid"></param>
    /// <param name="theaterid"></param>
    /// <param name="title"></param>
    /// <param name="detail"></param>
    public static bool AddMail(int senderId, int recieverId , DateTime time, string title, string detail)
    {
		int nT = 0;
		if (title != null)
			++nT;
		if (detail != null)
			++nT;
		nT += 3;
		string[] name = new string[nT];
		object[] value = new object[nT];
		name[0] = "SenderID";
		value[0] = senderId;
		name[1] = "Date";
		value[1] = time;
		name[2] = "RecieverID";
		value[2] = recieverId;
		nT = 2;
		if (title != null)
		{
			name[++nT] = "Title";
			value[nT] = title;
		}
		if (detail != null)
		{
			name[++nT] = "Detail";
			value[nT] = detail;
		}

		try
		{

			return DataBaseClass.insertData(name, value, "Mail", DataBaseClass.GetConnectionString());
		}
		catch (SqlException e)
		{
			System.Console.Write("Mail类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (ArgumentException e)
		{
			System.Console.Write("Mail类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (InvalidOperationException e)
		{
			System.Console.Write("Mail类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (Exception e)
		{
			System.Console.Write("Mail类：");
			System.Console.WriteLine("未知错误:" + e.Message);
			return false;
		}
       
    }
    //2)保存自身到数据库
    /// <summary>
    /// 更新mail
    /// </summary>
    /// <param name="mail"></param>
    public bool UpdateMail()
    {
		bool bT =  AddMail(this.SenderID,RecieverID,Date,Title,Detail);
		this.m_iID = DataBaseClass.GetMaxID("Mail" , _connectionString);
		return bT;
		// string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
		//string[] value = { mail.m_iSenderID.ToString(), mail.m_iRecieverID.ToString(), date, mail.m_sTitle, mail.m_sDetail };

		//DataBaseClass.insertData(value, "Mail", _connectionString);
    }
    

    //3)删除此站内信
    //void DelMail(id, RecieverID)
    /// <summary>
    /// 删除站内信
    /// </summary>
    /// <param name="id">站内信ID</param>
    /// <returns></returns>
    public static bool DelMail(int id)
    {
        bool succ = DataBaseClass.deleteData("ID", id, "Mail", _connectionString);
        return succ;

    }
    /// <summary>
    /// 查找某条站内信
    /// </summary>
    /// <param name="ID">站内信ID</param>
    /// <returns></returns>
    public static DataTable GetMail(int ID)
    {
		return DataBaseClass.selectData("SELECT * FROM Mail WHERE ID="+ ID , _connectionString);
        string sql = String.Empty;
        //if (type == "date")
        //{
        //        DateTime time = (DateTime)detail;
        //        DateTime now = DateTime.Now;
        //        sql = "SELECT * FROM T_Comment WHERE Date BETWEEN " + time.ToString() + " and " + now.ToString();
        //}
        //else if (type == "theater")
        //{
        //string movie = (string)detail;
        sql = "SELECT * FROM Mail WHERE TheaterID = " + ID + ";";
        // }
        //else if (type == "user")
        // {
        //         string user = (string)detail;
        //         sql = "SELECT * FROM T_Comment WHERE UserID = '" + user + "';";
        //}


        SqlDataAdapter dad = new SqlDataAdapter(sql, _connectionString);
        DataTable dtblMCom = new DataTable();
        dad.Fill(dtblMCom);

        return dtblMCom;
    }
    static Mail()
    {
		_connectionString = DataBaseClass.GetConnectionString();
    }

    public static List<Mail> GetListFromDataTable(DataTable dt)
    {
        List<Mail> temp = new List<Mail>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            Mail mail = new Mail((int)dt.Rows[i]["ID"], (int)dt.Rows[i]["SenderID"]
                , (int)dt.Rows[i]["RecieverID"], (DateTime)dt.Rows[i]["Date"]
                , dt.Rows[i]["Title"] as string, dt.Rows[i]["Detail"] as string);
            temp.Add(mail);
        }
        return temp;
    }
	public bool DelMa_SelfItem()
	{
		return DelMail(this.ID);
	}
}
